﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class LoadGame : MonoBehaviour {
    public InputField UserName;
    public InputField PassWord;
    public GameObject LoginPanel;
    public GameObject LogonPanel;
    public GameObject ErrorPanel;

    private string NowGamer;

    public void LoadTheGame()
    {
        SceneManager.LoadScene(2);
    }

    public void ExitGame()
    {
        Application.Quit();
    }

    public void LoadRankingList()
    {
        SceneManager.LoadScene(4);
    }

    public void GoShop()
    {
        SceneManager.LoadScene(3);
    }

    void Awake()
    {
        NowGamer = GameObject.Find("Main Camera").GetComponent<GameID>().nowGamer;
        if (NowGamer == null||NowGamer=="")
        {
            LoginPanel.SetActive(true);
        }
        else
        {
            LoginPanel.SetActive(false);
        }
        for (int i = 9; i >= 0; i--)
        {
            if ((PlayerPrefs.GetInt("@MaxScore" + i.ToString()) <=0))
            {
                PlayerPrefs.SetInt("@MaxScore" + i.ToString(), 0);
            }
            else break;
        }
    }
    

    public bool isLegal(string tmp)
    {
        if (tmp.Length >= 10) return false;
        for (int i = 0; i < tmp.Length; i++)
        {
            if (!(tmp[i] >= '1' && tmp[i] <= '9') && !(tmp[i] >= 'a' && tmp[i] <= 'z') &&
                !(tmp[i] >= 'A' && tmp[i] <= 'Z') && tmp[i] != '_')
                return false;
            if (i == 0 && tmp[0] >= '0' && tmp[0] <= '9')
            {
                return false;
            }
        }

        return true;
    }

    public void setMoney(float money)
    {
        PlayerPrefs.SetFloat(NowGamer + "Money", money);
    }

    public void setShop(int Shop)
    {
        PlayerPrefs.SetInt(NowGamer + "Shop", Shop);
    }

    //注册
    public void Logon()
    {
        string Name = UserName.text.ToString();
        string Pass = PassWord.text.ToString();
        if (Name == "" || Pass == "") return;
        if (!PlayerPrefs.HasKey(Name) && Name[0] != '@' && isLegal(Name) && (Pass.Length >= 6))
        {
            PlayerPrefs.SetString(Name, Pass);
            setMoney(0f);
            setShop(0);
        }
        else
        {
            LogonPanel.SetActive(true);
        }
    }
    //登陆
    public void Login()
    {
        string Name = UserName.text;
        string Pass = PassWord.text;
        if (Name == "" || Pass == "") return;
        if (isLegal(Name) && PlayerPrefs.HasKey(Name) && PlayerPrefs.GetString(Name) == Pass && Name[0] != '@')
        {
            NowGamer = Name;
            GameObject.Find("Main Camera").GetComponent<GameID>().nowGamer = Name;
            LoginPanel.SetActive(false);
        }
        else
        {
            ErrorPanel.SetActive(true);
        }
    }


    public void ReturnLogin()
    {
        ErrorPanel.SetActive(false);
        LogonPanel.SetActive(false);
    }

}
